//
//  MusicListController.m
//  HB
//
//  Created by 陶秋安 on 16/2/29.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "MusicListController.h"
#import "MusicListOperation.h"
#import "MusicListTableViewCell.h"
#import "ZYMusicTool.h"

@implementation MusicListController

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    [Common setCover:self.albumBg];
    
    self.djHeadImg.layer.cornerRadius=15;
    
    self.djHeadImg.clipsToBounds=true;
    
    self.tabMusicList.delegate=self;
    
    self.tabMusicList.dataSource=self;
    
    [self loadAlbumInfo];
    
    self.albumTitle.lineBreakMode = NSLineBreakByTruncatingTail;
    
    [ZYMusicTool  clear];
    
    currPageIndex= 1;
    
    pageSize= 10;
    
    [self loadMusicListData:currPageIndex andSize:pageSize];
    
    
    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.tabMusicList attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant: DeviceHeight-190];
    
    [_viewContent addConstraint:heightConstraint];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)btnBack:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void) loadAlbumInfo
{
    operation= [[[MusicListOperation alloc] init] initWithDelegate:self];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Recommend" andFuncName:@"AlbumInfo"];
    
    NSDictionary * dictInfo = @{@"url":[NSString stringWithFormat:@"%@?albumID=%ld",URL,albumID]};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:0];
}

-(void) loadMusicListData:(NSInteger) pageindex andSize:(NSInteger) pagesize
{
    operation= [[[MusicListOperation alloc] init] initWithDelegate:self];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Recommend" andFuncName:@"MusicListByAlbumID"];
    
    NSDictionary * dictInfo = @{@"url":[NSString stringWithFormat:@"%@?albumID=%ld&pageindex=%ld&pagesize=%ld",URL,albumID,(long)pageindex,(long)pagesize]};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if(flag==0)
    {
        
        if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
        {
            NSArray *arrayData= [dataParse objectForKey:@"data"];
            
            for (NSInteger i=0; i<[arrayData count]; i++)
            {
                NSDictionary *dict= [arrayData objectAtIndex:i];
                
                NSString *coverPath= [dict objectForKey:@"CoverPath"];
                
                NSString *coverName= [dict objectForKey:@"CoverName"];
                
                [Common downLoadImageByURL:self.albumCover andURL:coverPath andName:coverName];
                
                [Common downLoadImageByURL:self.albumBg andURL:coverPath andName:coverName];
                
                self.djName.text=[dict objectForKey:@"djName"];
                
                NSString *HeadImg= [dict objectForKey:@"HeadImg"];
                
                NSString *HeadImgName= [dict objectForKey:@"HeadImgName"];
                
                [Common downLoadImageByURL:self.djHeadImg andURL:HeadImg andName:HeadImgName];
                
                self.albumTitle.text=[dict objectForKey:@"Title"];
                
            }
            
        }
        
        
    }
    else if(flag ==1)
    {
        //[ZYMusicTool clear];
        
        if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
        {
             NSInteger totlePageCount= [[dataParse objectForKey:@"totlePageCount"] intValue];
            
             currPageIndex= [[dataParse objectForKey:@"currPageIndex"] intValue];
            
            if(totlePageCount>1 && currPageIndex < totlePageCount)
            {
                _hasNextPage=YES;
            }
            else
            {
                _hasNextPage= NO;
            }
            
            NSArray *arrayData= [dataParse objectForKey:@"data"];
            
            
            for (NSInteger i=0; i<[arrayData count]; i++)
            {
                
                NSDictionary *dict= [arrayData objectAtIndex:i];
                
                ZYMusic *music=[[ZYMusic alloc] init];
                
                music.name=[dict objectForKey:@"MusicName"];
                
                music.icon=[dict objectForKey:@"Icon"];
                
                music.iconName=[dict objectForKey:@"IconName"];
                
                music.musicListArray=[dict objectForKey:@"musiclist"];
                
                music.lrcname=@"";
                
                music.singer=[dict objectForKey:@"singer"];
                
                music.djID=[dict objectForKey:@"djID"];
                
                music.musicID=[dict objectForKey:@"MusicID"];
                
                music.singerIcon=[dict objectForKey:@"singericon"];
                
                [ZYMusicTool addMusics:music];
                
            }

        }
        
       // self.lblSumCount.text= [NSString stringWithFormat:@"共%ld首",[[ZYMusicTool musics] count]];
        
        [self.tabMusicList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
}


//设置表格有几个数据区域
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num=[[ZYMusicTool musics] count];
    
    if(num>0)
    {
        return num + (_hasNextPage?2:1);
    }
    else
    {
        return 0;
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row==0)
    {
        UITableViewCell *cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        
        cell.textLabel.text=[NSString stringWithFormat:@"共%lu首",(unsigned long)[[ZYMusicTool musics] count]];
        
        cell.textLabel.font =[UIFont systemFontOfSize:15];
        
        return  cell;
    }
    else
    {
        if(indexPath.row -1 <[[ZYMusicTool musics] count])
        {
            NSString *idefine = @"tabMusicListCell";
            
            MusicListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idefine];
            
            if (cell==nil)
            {
                cell = [[MusicListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idefine];
            }
            
            cell.selectionStyle= UITableViewCellSelectionStyleNone;
            
            // 次方法是在拖动表格时 单元格显示在表格的bounds 范围内时触发
            
            NSInteger i=indexPath.row-1;
            
            if (i< [[ZYMusicTool musics] count])
            {
                ZYMusic *music= [[ZYMusicTool musics] objectAtIndex:i];
                
                cell.number.text = [NSString stringWithFormat:@"%ld",i+1];
                
                cell.lblAuthorInfo.text = music.singer;
                
                cell.lblMucisName.text = music.name;
                
            }
            
            return cell;
        }
        else
        {
            
            NSString  *cellIdentifier = @"NewsMoreCell";
            
            MoreCell *cell = (MoreCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            
            currPageIndex=currPageIndex+1;
            
            [self loadMusicListData:currPageIndex andSize:pageSize] ;//加载下一页数据
            
            if (cell == nil) {
                
                NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
                
                cell = [Objects objectAtIndex:0];
                
            }
            
            cell.selectionStyle= UITableViewCellSelectionStyleNone;
            
            return cell;
            
            
        }
        
    }
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ZYMusic *music= [[ZYMusicTool musics] objectAtIndex:indexPath.row-1];
    
    if(![[ZYMusicTool playingMusic].filename isEqualToString:music.name])
    {
        [ZYMusicTool playingMusic].playing=NO;
        
        [ZYMusicTool setPlayingMusic:music];
    }
    
    [self musicPlay].playMode=@"more";
    
    [self addViewRes:music];
    
    [self presentViewController:[self musicPlay] animated:true completion:nil];
    
}

@end
